import java.util.ArrayList;
import java.awt.Rectangle;

public class Test {

	public static void main(String args[]){
		new SpriteLib(); // necessary for Defender declaration to work.
		
//		testProjectilePruning();
		testLocalSpace();
	}
	
	public static void testLocalSpace(){
		
		Defender def = new Defender();
//		Rectangle dRect = new Rectangle(528, 450, 38, 16); no, we want to be centered there
//		Rectangle dRect = new Rectangle(509, 448, 38, 10);
		ArrayList<Projectile> lasers = new ArrayList<Projectile>();
		
//		Rectangle dRect = new Rectangle(229, 448, 38, 10);
//		lasers.add(new Projectile(276, 452, 8, SpriteLib.INVADER_LASER, null));
		
		Rectangle dRect = new Rectangle(85, 448, 38, 10);
		lasers.add(new Projectile(132, 460, 8, SpriteLib.INVADER_LASER, null));
		
		
		Defender.LocalSpace ls = def.new LocalSpace(lasers, dRect);

//		say("below? " + ls.testBelow());
//		say("hit? " + ls.testHit());
		int[] offsets = {8, 0, -8};
		say("final move offset: " + ls.evaluateMove(offsets, 5));
	}
	
	public static void testProjectilePruning(){
		Rectangle def = new Rectangle(128, 450, 38, 16);
		
		ArrayList<Projectile> lasers = new ArrayList<Projectile>();
		lasers.add(new Projectile(148, 444, 8, SpriteLib.INVADER_LASER, null));
		
		/* Now, remove any projectiles that are
		 * - Owned by the Defender
		 * - Already below the Defender
		 * - Going to hit a Base
		 * - Far enough above or to the side that it won't affect the Defender in the next 5 steps
		 *  (5 steps will change in predictive power if the framerate is altered, true. Oh well)
		 */
		for(int i = 0; i < lasers.size(); i++){
			// first, compile all necessary statistics and other info about the given lazer
			Projectile laz = lasers.get(i);
			
			say("-Position:\t(" + laz.x_position + ", " + laz.y_position + ")");
			int foresight = 5;
			
			////////////// info about juxtaposition with the Defender
			int contactHeight = def.y -def.height/2 - laz.height/2;
			// technically, contactHeight will be 6px lower due to the diminished Defender hitbox, but whatever
			
			int safeHeight = contactHeight - laz.getVelocity()*foresight;
			int safeHDistance = laz.width/2 + def.width/2 + Defender.MOVE_SPEED*foresight;
			Boolean tooFarAway = Math.abs(def.x - laz.x_position) > safeHDistance;
			Boolean belowDefender = ((def.y + def.height/2) - (laz.y_position - laz.height/2)) < laz.getVelocity();
			say("def.y = " + def.y);
			say("def.height = " + def.height);
			say("def.y + this.height/2 = " + (def.y + def.height/2));
			say("laz.y_position - laz.height/2 = " + (laz.y_position - laz.height/2));
			
			////////////// info about base hittage
			Boolean aboveBase = false;
			// make a Rectangle with the width and x_position of laz, but extending vertically through the whole gamespace
			Rectangle lRect = new Rectangle(laz.getRect());
			lRect.y = 0;
			lRect.height = InvaderSaver.getGameHeight();
			
			//////////////  now, see if laz would hit any bases
//			for(Base b: bases){
//				if(lRect.intersects(b.getRect())){
//					aboveBase = true;
//				}
//			}
			
//			say("owner: " + laz.getOwner());
			say("below: " + belowDefender);
//			say("abovebase: " + aboveBase);
			say("too high: " + (safeHeight > laz.y_position));
			say("far away: " + tooFarAway);
			
			//////////////// now take the previously-gathered stats and do the actual pruning
			if(laz.getOwner() == ProjectileControl.Owner.DEFENDER || belowDefender || aboveBase || safeHeight > laz.y_position || tooFarAway){
				say("Pruning!");
				lasers.remove(i);
				i--; // to account for removing one of the objects
				continue;
			}
		} // end of projectile-pruning for loop
	}
	
	public static void say(String text){
		System.out.println(text);
	}
}
